<?php
	/**
	 * 用户登录注册控制器
	 */
	class UcenterAction extends Action{

		//用户登录
		public function index(){
			$this->display();
		}
		//处理用户登录
		public function login(){
			if($this->isPost()){
				$where = array(
					'username' => $this->_post('uname'),
					'password' => md5($this->_post('ups'))
					);
				$db = M('user');
				if($uid = $db->where($where)->find()){
					$_SESSION['uname'] = $this->_post('uname');
					$_SESSION['uid'] = $uid;
					echo '<script>location.href="'.__ROOT__.'/index.php"</script>';
				}else{
					$this->error('登录失败请重试');
				}
			}
		}
		//处理用户注册
		public function register(){
			if($this->isPost()){
				$data = array(
					'username' => $this->_post('rgname'),
					'password' => md5($this->_post('rgps')),
					'email' => $this->_post('rgemail')
					);
				$db = M('user');
				if($uid = $db->add($data)){
					$_SESSION['uname'] = $this->_post('rgname');
					$_SESSION['uid'] = $uid;
					$this->success('注册用户成功！' , U('welcome'));	
				}else{
					$this->error('注册用户失败，请重试！');
				}
			}
		}

		//欢迎页面
		public function welcome(){
			$this->display();
		}

		//用户退出
		public function logout(){			
			if($this->isGet()){
				unset($_SESSION['uid']);
				unset($_SESSION['uname']);
				if(!isset($_SESSION['uname'])){
					$this->success('退出成功！','__ROOT__/index.php');
				}
			}
		}
	}
?>